#include <config.h>
#include <board.h>
#include <init.h>
#include <os.h>
#include <bitmap.h>
#include <bitops.h>
#include <settings.h>
#include "ES8P508x.h"
#include "lib_config.h"
#include "device.h"
#include "printk.h"
#include "es8p508_buz.h"


static struct device buz_dev;

static void buz_on(void)
{
    GPIO->BUZC.BUZEN = 1;
}
static void buz_off(void)
{
    GPIO->BUZC.BUZEN = 0;
}

static void buz_tmr_cb(struct soft_timer *st)
{
	buz_off();
}

static struct soft_timer buz_tmr =
{
    .expires = INITIAL_JIFFIES,
    .cb      = buz_tmr_cb,
};
static void buz_gpio_init(void);
static err_t buz_ctrl(device_t *dev, uint8_t cmd, void *args)
{
    switch (cmd)
    {
    case CTRL_ON:
    {
		buz_on();
		buz_tmr.expires = jiffies + configHZ / 5;
		soft_timer_add(&buz_tmr);
    }
    break;
    }
    return 0;
}

static struct device_ops buz_ops =
{
    //.init = buz_init,
    .ctrl = buz_ctrl,
};

static void buz_gpio_init(void)
{
    GPIO_InitStruType y;

	y.GPIO_Signal = GPIO_Pin_Signal_Digital;
    y.GPIO_Func = GPIO_Func_3;
    y.GPIO_Direction = GPIO_Dir_Out;
    y.GPIO_PUEN = GPIO_PUE_Input_Disable;
    y.GPIO_PDEN = GPIO_PDE_Input_Disable;
    y.GPIO_OD = GPIO_ODE_Output_Disable;
	y.GPIO_DS = GPIO_DS_Output_Normal;
    GPIO_Init(BUZ_PORT,BUZ_PIN,&y);
	
	GPIO->BUZC.BUZ_LOAD = 9999;         //F = Fpclk/(2*(BUZ_LOAD + 1))  --2k
}

int board_setup_buz(void)
{
    buz_gpio_init();

    buz_dev.ops = &buz_ops;
    device_register(&buz_dev, "buz", 0);

    return 0;
}
//device_initcall(board_setup_buz);
